//==============================================================================
//
// Title:       TXFbPathLossOrGain.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-5-8 at 16:30:11 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

#include "RFBB.h"         
#include <utility.h>
#include <formatio.h>	  
#include "hpesg.h"
#include "SetSignalGenerator.h"



//FMON_ACTION_SYNTHESIZER                              
_SHMON_TX_FB_LOSS_GAIN_CMD g_szShmonRTXFbPathLossOrGainCmd[4] ={
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_FB_path_loss_gain_Pipe1.txt" ,"TX_FB_path_loss_gain_Pipe1.txt",0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_FB_path_loss_gain_Pipe2.txt" ,"TX_FB_path_loss_gain_Pipe2.txt",0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_FB_path_loss_gain_Pipe3.txt" ,"TX_FB_path_loss_gain_Pipe3.txt",0.0},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_FB_path_loss_gain_Pipe4.txt" ,"TX_FB_path_loss_gain_Pipe4.txt",0.0}};

/**************************************************************************************************
  Function:        RFTestLoopAttenuator
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST TXFbPathLossOrGain(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    double dTestNum = 0.0;
    double dFrequency = 0.0;
    double dFbGain = 0.0;
    int  error = 0;
    int  iPassFail = 0;
    int i = 0;
    int iPipeNum = 0;
    int iReturn = 0;
    
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestFrequency", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dFrequency));

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestNum", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dTestNum));

    if((dTestNum >= 0) && (dTestNum < 4))
    {
        i = dTestNum;
    }

     //Set signal generation
    iReturn =SetupEsgSignalBbgDLWcdma(0); 
    hpESG_SetFrequencyCW (g_hSignalGenerator, (dFrequency * 1E6));
    hpESG_SetPower( g_hSignalGenerator, 10.0, hpESG_UNIT_DBM );   
    hpESG_SetPowerOn (g_hSignalGenerator, VI_TRUE);
    
    //--------------------------------------------------------------------
    iReturn = RfbbInetFTPAutoSend(g_szDUTIP, "", "", g_szShmonRTXFbPathLossOrGainCmd[i].cLocalFilePath, g_szShmonRTXFbPathLossOrGainCmd[i].cRemoteFileName, INET_FTP_FILE_TYPE_BINARY);    
    if(iReturn < 0)
    {
        sprintf( szLogMsg, STRING_MESSAGE_Ftp_File_Err, g_szDUTIP, InetGetErrorMessage( iReturn ) );
        EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        
        goto Error;
    } 

    g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 

    //run script    
    sprintf(kszShmonAgent,". %s",g_szShmonRTXFbPathLossOrGainCmd[i].cRemoteFileName);
    Shmon_RunScript(kszShmonAgent,5); 

    
    Delay(10);
    
    //send shell command and compare return value 
    iPipeNum = i;
    sprintf(kszShmonAgent,"dapd -stat %d",(iPipeNum + 1));
    ErrChk(ShellCommand_Numeric(kszShmonAgent, -1,"PDRX power............ ",&g_szShmonRTXFbPathLossOrGainCmd[i].dGetFbValue)); 
    sprintf( szLogMsg, "the PDRX power is %f" ,g_szShmonRTXFbPathLossOrGainCmd[i].dGetFbValue);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);    
    dFbGain = g_szShmonRTXFbPathLossOrGainCmd[i].dGetFbValue - 10.0 + 1.05;
    
Error:
    
#if(ON_OFF != SIMU_VERS) 

    hpESG_SetPowerOn (g_hSignalGenerator, VI_FALSE);

    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }

    testData->measurement = dFbGain;

    InetTelnetClose(g_hShellCommand); 
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}

